﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_TaiLieu : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai báo bien
        TaiLieu_BLL tl_bll = new TaiLieu_BLL();
        TextMessageBox box = new TextMessageBox();
        DataTable dt = new DataTable();
        bool isTrangThai=false;
        #endregion

        #region LoadForm

        public uc_TaiLieu()
        {
            InitializeComponent();
        }

        private void uc_TaiLieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region cac chuc nang

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmTaiLieu frm = new frmTaiLieu();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frmTaiLieu frm = new frmTaiLieu();
                    frm.maTaiLieu = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaTaiLieu).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadData();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter, box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);

        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                TaiLieu_Public tl = new TaiLieu_Public();
                try
                {
                    string ma = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaTaiLieu).ToString();
                    tl.MaTaiLieu = ma;
                    string ten = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colNhanDe).ToString();
                    dt = tl_bll.TaoBang(box.sqlCheckExist("DangKyCaBiet", "MaTaiLieu", ma));
                    if (dt.Rows.Count > 0)
                    {
                        XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            tl_bll.Delete(tl);
                            LoadData();
                        }
                        catch (Exception)
                        {
                            XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
        private void btnDKMCaBiet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    string ma = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaTaiLieu).ToString();
                    string ten = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colNhanDe).ToString();
                    dt = tl_bll.TaoBang("select MaTaiLieu  from TaiLieu where MaTaiLieu=N'" + ma + "' and TrangThaiDKCB=0");

                    if (dt.Rows.Count > 0)
                    {
                        frmDangKyMaCaBiet frm = new frmDangKyMaCaBiet();
                        frm.maTaiLieu = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaTaiLieu).ToString();
                        if (frm.ShowDialog() == DialogResult.OK)
                            LoadData();
                    }
                    else
                        XtraMessageBox.Show(string.Format("Tài Liệu:{0} đã được Đăng ký mã cá biệt.", ten), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show("Không có dữ liệu để Đăng ký mã cá biệt.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        #region ham viet them

        private void LoadData()
        {
            TaiLieu_Public tl = new TaiLieu_Public();
            tl.MaTaiLieu = txtMaTaiLieu.Text.Trim();
            tl.NhanDe = txtNhanDe.Text;
            tl.TacGiaChinh = txtTacGiaChinh.Text;
            tl.NamXuatBan =box.ToInt(txtNamXuatBan.Text);
            tl.NgayBienMuc = dtpTuNgayFrom.Value;
            tl.TrangThaiDKCB = isTrangThai;
            grd_Data.DataSource = tl_bll.LayDuLieu(tl,dtpDenNgayTo.Value);
            grv_Data.BestFitColumns();
        }

        private void btnTimKiem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
        #endregion


        #region cac su kien

        private void ckbTrangTHai_CheckedChanged(object sender, EventArgs e)
        {
            if(ckbTrangTHai.Checked)
                isTrangThai=true;
            else
                isTrangThai=false;
        }

        #endregion

    }
}
